package com.pointlion.sys.mvc.admin.evaluate.paperuser;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.pointlion.sys.mvc.common.model.PaperUser;
import com.pointlion.sys.mvc.common.model.SysUser;
import com.pointlion.sys.plugin.shiro.ShiroKit;

public class PaperUserService{
	public static final PaperUserService me = new PaperUserService();
	public static final String TABLE_NAME = PaperUser.tableName;
	
	/***
	 *
	 */
	public PaperUser getById(String id){
		return PaperUser.dao.findById(id);
	}
	
	/***
	 *
	 */
	public Page<Record> getPage(int pnum,int psize,String starttime,String endtime,String search){
		String sql  = " from (select p.*,sum(u.total_value)/p.paperCount as total,p.create_time as createTime from "+TABLE_NAME+" u left join paper p on u.paper_id = p.id where 1=1 ";
		if(StrKit.notBlank(starttime)){
			sql += " and date_format(p.create_time,'%Y-%m-%d') >= '"+starttime+"'";
		}
		if(StrKit.notBlank(endtime)){
			sql += " and date_format(p.create_time,'%Y-%m-%d') <= '"+endtime+"'";
		}
		if(StrKit.notBlank(search)){
			sql += " and p.paperTitle like '%"+search+"%' ";
		}
		SysUser u = SysUser.dao.getById(ShiroKit.getUserId());
		if("1".equals(u.getIsAdmin())){

		}else{
			sql += " and u.create_id =  '"+ShiroKit.getUserId()+"' ";
		}
		sql += " group by paper_id order by u.create_time desc) a  left join sys_user su on a.userId = su.id ";
		return Db.paginate(pnum, psize, " select a.*,su.name ", sql);
	}
	
	/***
	 * ɾ��
	 * @param ids
	 */
	@Before(Tx.class)
	public void deleteByIds(String ids){
    	String idarr[] = ids.split(",");
    	for(String id : idarr){
    		PaperUser o = me.getById(id);
    		o.delete();
    	}
	}
	
}